//Dom4jTest.java
package com.ylaihui.pojo;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.junit.Test;

import java.util.List;

public class Dom4jTest {
    @Test
    public void dom4jtest() throws DocumentException {
        SAXReader saxReader = new SAXReader();

        Document document = saxReader.read("Product.xml");

        System.out.println(document);
    }

    @Test
    public void test1() throws DocumentException {
        // dom4j的使用，从XML转换为Java的对象
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read("Product.xml");

        Element rootElement = document.getRootElement();

        List<Element> prds = rootElement.elements("prd");

        for (Element prd : prds) {
            Element nameelem = prd.element("name");
            String name = nameelem.getText();

            String price = prd.elementText("price");

            String sn = prd.attributeValue("sn");

            Product product = new Product(sn,name,Double.parseDouble(price));
            System.out.println(product);
        }
    }
}
